package qlsl.androiddesign.http.service.subservice;

import java.util.HashMap;
import java.util.List;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import qlsl.androiddesign.constant.MessageConstant;
import qlsl.androiddesign.constant.WhatConstant;
import qlsl.androiddesign.entity.commonentity.Pager;
import qlsl.androiddesign.entity.otherentity.PostingsGoods;
import qlsl.androiddesign.entity.otherentity.ShoppingDetail;
import qlsl.androiddesign.handler.subhandler.HttpHandler;
import qlsl.androiddesign.http.HttpListener;
import qlsl.androiddesign.http.service.baseservice.BaseService;
import qlsl.androiddesign.http.xutils.HttpProtocol;
import qlsl.androiddesign.method.UserMethod;
import qlsl.androiddesign.thread.HandlerThread;
import qlsl.androiddesign.util.commonutil.Log;
import qlsl.androiddesign.util.commonutil.PagerUtils;
import qlsl.androiddesign.util.runfeng.ThreadManager;
import qlsl.androiddesign.view.baseview.FunctionView;

/**
 * 签到模块 <br/>
 * 
 */
public class SignService extends BaseService {

	private static String className = getClassName(SignService.class);

	public static void createSign(final FunctionView<?> functionView,
			final HttpListener listener) {
		final HttpHandler handler = getHandler(functionView, listener,
				className, "createSign");

		ThreadManager.getThreadPool(className, "createSign").execute(
				new Runnable() {

					@Override
					public void run() {
						HttpProtocol protocol = new HttpProtocol();
						try {
							JSONObject jo = protocol
									.setMethod("signin")
									.removeUserParam()
									.addParam("fuserid",
											UserMethod.getUser().getUserId())
									.post();

							if (isDataInvalid(jo, handler, protocol)) {
								return;
							}

							handler.sendMessage(protocol,
									WhatConstant.WHAT_NET_DATA_SUCCESS,
									MessageConstant.MSG_EMPTY);
						} catch (Exception e) {
							e.printStackTrace();
							handler.sendMessage(protocol,
									WhatConstant.WHAT_EXCEPITON, e);
						}
					}
				});
	}

	public static void queryPointsGoodsList(final int page,
			final FunctionView<?> functionView, final HttpListener listener) {
		final HttpHandler handler = getHandler(functionView, listener,
				className, "queryPointsGoodsList");

		ThreadManager.getThreadPool(className, "queryPointsGoodsList").execute(
				new Runnable() {

					@Override
					public void run() {
						HttpProtocol protocol = new HttpProtocol();
						try {
							JSONObject jo = protocol
									.setMethod("getpointsgoodslist")
									.removeUserParam()
									.addParam("fuserid",
											UserMethod.getUser().getUserId())
									.post();
							if (isDataInvalid(jo, handler, protocol)) {
								return;
							}
							HashMap<String, Object> map = new HashMap<String, Object>();

							// PostingsGoods
							List<ShoppingDetail> list = JSONArray.parseArray(
									jo.getString("rows"), ShoppingDetail.class);

							Pager pager = PagerUtils.createPager(page, jo);
							map.put("list", list);
							map.put("pager", pager);

							handler.sendMessage(protocol,
									WhatConstant.WHAT_NET_DATA_SUCCESS, map);
						} catch (Exception e) {
							e.printStackTrace();
							handler.sendMessage(protocol,
									WhatConstant.WHAT_EXCEPITON, e);
						}
					}
				});
	}
}
